home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy-1.1 (sources only) / mindy-1.1 / libraries / dylan / cond.dylan < prev    next >
Encoding:
Text File  |  1994-06-28  |  6.1 KB  |  266 lines  |  [TEXT/ttxt]

  1. module: Dylan
  2. rcs-header: $Header: cond.dylan,v 1.6 94/06/27 17:10:20 wlott Exp $
  3.  
  4. //======================================================================
  5. //
  6. // Copyright (c) 1994  Carnegie Mellon University
  7. // All rights reserved.
  8. // 
  9. // Use and copying of this software and preparation of derivative
  10. // works based on this software are permitted, including commercial
  11. // use, provided that the following conditions are observed:
  12. // 
  13. // 1. This copyright notice must be retained in full on any copies
  14. //    and on appropriate parts of any derivative works.
  15. // 2. Documentation (paper or online) accompanying any system that
  16. //    incorporates this software, or any part of it, must acknowledge
  17. //    the contribution of the Gwydion Project at Carnegie Mellon
  18. //    University.
  19. // 
  20. // This software is made available "as is".  Neither the authors nor
  21. // Carnegie Mellon University make any warranty about the software,
  22. // its performance, or its conformity to any specification.
  23. // 
  24. // Bug reports, questions, comments, and suggestions should be sent by
  25. // E-mail to the Internet address "gwydion-bugs@cs.cmu.edu".
  26. //
  27. //======================================================================
  28. //
  29. // This file implements the condition system.
  30. //
  31.  
  32.  
  33. // Classes
  34.  
  35. define class <condition> (<object>)
  36. end;
  37.  
  38. define class <serious-condition> (<condition>)
  39. end;
  40.  
  41. define class <error> (<serious-condition>)
  42. end;
  43.  
  44. define class <simple-condition> (<condition>)
  45.   slot condition-format-string,
  46.     required-init-keyword: format-string:;
  47.   slot condition-format-arguments,
  48.     init-keyword: format-arguments:,
  49.     init-value: #();
  50. end;
  51.  
  52. define class <simple-error> (<error>, <simple-condition>)
  53. end;
  54.  
  55. define class <type-error> (<error>)
  56.   slot type-error-value, init-keyword: value:;
  57.   slot type-error-expected-type, init-keyword: type:;
  58. end;
  59.  
  60. define class <warning> (<condition>)
  61. end;
  62.  
  63. define class <simple-warning> (<warning>, <simple-condition>)
  64. end;
  65.  
  66. define class <restart> (<condition>)
  67. end;
  68.  
  69. define class <simple-restart> (<restart>, <simple-condition>)
  70. end;
  71.  
  72. define class <abort> (<restart>)
  73.   slot abort-description :: <byte-string>,
  74.     init-keyword: description:,
  75.     init-value: "<abort>";
  76. end;
  77.  
  78.  
  79. // Condition reporting.
  80.  
  81. define method report-condition (condition :: <condition>)
  82.   prin1(condition);
  83. end;
  84.  
  85. define method report-condition (condition :: <simple-condition>)
  86.   apply(format,
  87.     condition.condition-format-string,
  88.     condition.condition-format-arguments);
  89. end;
  90.  
  91. define method report-condition (condition :: <type-error>)
  92.   format("%= is not of type %=",
  93.      condition.type-error-value,
  94.      condition.type-error-expected-type);
  95. end;
  96.  
  97. define method report-condition (condition :: <abort>)
  98.   puts(condition.abort-description);
  99. end;
  100.  
  101.  
  102. // Condition signaling
  103.  
  104. define method signal (string :: <string>, #rest arguments)
  105.   signal(make(<simple-warning>,
  106.           format-string: string,
  107.           format-arguments: arguments));
  108. end;
  109.  
  110. define method signal (cond :: <condition>, #rest noise)
  111.   unless (empty?(noise))
  112.     error("Can only supply format arguments when supplying a format string.");
  113.   end;
  114.   local
  115.     method search (h)
  116.       if (h)
  117.     if (instance?(cond, h.handler-type))
  118.       let test = h.handler-test;
  119.       if (~test | test(cond))
  120.         let remaining = h.handler-next;
  121.         h.handler-function(cond, method () search(remaining) end);
  122.       else
  123.         search(h.handler-next);
  124.       end;
  125.     else
  126.       search(h.handler-next);
  127.     end;
  128.       else
  129.     default-handler(cond);
  130.       end;
  131.     end;
  132.   search(current-handler());
  133. end;
  134.  
  135. define method error (string :: <string>, #rest arguments)
  136.   error(make(<simple-error>,
  137.          format-string: string,
  138.          format-arguments: arguments));
  139. end;
  140.  
  141. define method error (cond :: <condition>, #rest noise)
  142.   unless (empty?(noise))
  143.     error("Can only supply format arguments when supplying a format string.");
  144.   end;
  145.   signal(cond);
  146.   invoke-debugger(make(<simple-error>,
  147.                format-string:
  148.              "Attempt to return from a call to error"));
  149. end;
  150.  
  151. define method cerror (restart-descr, cond-or-string, #rest arguments)
  152.   block ()
  153.     apply(error, cond-or-string, arguments);
  154.   exception (<simple-restart>,
  155.          init-arguments: list(format-string: restart-descr,
  156.                   format-arguments: arguments))
  157.     #f;
  158.   end;
  159. end;
  160.  
  161. define method type-error (value, type)
  162.   error(make(<type-error>, value: value, type: type));
  163. end;
  164.  
  165. define method check-type (value, type)
  166.   if (instance?(value, type))
  167.     value;
  168.   else
  169.     type-error(value, type);
  170.   end;
  171. end;
  172.  
  173. define method abort ()
  174.   error(make(<abort>));
  175. end;
  176.  
  177. define method default-handler (condition :: <condition>)
  178.   #f;
  179. end;
  180.  
  181. define method default-handler (condition :: <serious-condition>)
  182.   invoke-debugger(condition);
  183. end;
  184.  
  185. define method default-handler (condition :: <warning>)
  186.   report-condition(condition);
  187.   #f;
  188. end;
  189.  
  190. define method default-handler (restart :: <restart>)
  191.   error("No restart handler for %=", restart);
  192. end;
  193.  
  194.  
  195. // Breakpoints.
  196.  
  197. define class <breakpoint> (<simple-warning>)
  198. end;
  199.  
  200. define method return-allowed? (cond :: <breakpoint>)
  201.   #t;
  202. end;
  203.  
  204. define method return-query (cond :: <breakpoint>)
  205.   #f;
  206. end;
  207.  
  208. define method return-description (cond :: <breakpoint>)
  209.   "Return #f";
  210. end;
  211.  
  212. define method %break (string :: <string>, #rest arguments)
  213.   %break(make(<breakpoint>,
  214.           format-string: string,
  215.           format-arguments: arguments));
  216. end;
  217.  
  218. define method %break (cond :: <condition>, #rest noise)
  219.   unless (empty?(noise))
  220.     error("Can only supply format arguments when supplying a format string.");
  221.   end;
  222.   block ()
  223.     invoke-debugger(cond);
  224.   exception (<simple-restart>,
  225.          init-arguments: list(format-string: "Continue from break"))
  226.     #f;
  227.   end;
  228. end;
  229.  
  230. define method break (#rest arguments)
  231.   if (empty?(arguments))
  232.     %break("Break.");
  233.   else
  234.     apply(%break, arguments);
  235.   end;
  236. end;
  237.  
  238.  
  239. // Introspection.
  240.  
  241. define method do-handlers (function :: <function>)
  242.   for (h = current-handler() then h.handler-next,
  243.        while h)
  244.     function(h.handler-type,
  245.          h.handler-test | method (x) #t end,
  246.          h.handler-function,
  247.          h.handler-init-args);
  248.   end;
  249. end;
  250.  
  251. define method return-allowed? (cond :: <condition>)
  252.   #f;
  253. end;
  254.  
  255. define generic return-description (cond);
  256.  
  257.  
  258. // Interactive handling.
  259.  
  260. define method restart-query (restart :: <restart>)
  261.   #f;
  262. end;
  263.  
  264. define generic return-query (condition);
  265.  
  266.